<?php

require_once ('common.php');
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$cookielength = ($_POST['cookielength'] == '-1') ? 31536000 : (int)$_POST['cookielength'] * 60;
if (!$user['is_guest'])
{
    header('location: ' . APP_URL);
    exit;
}
if (!empty($username) && !empty($password))
{
    $pass_encode = sha1(md5($password));
    $member = $db->query('SELECT uid, username FROM bbs_members WHERE username = %1 AND password = %2', array($username, $pass_encode));
    if (!empty($member))
    {
        $session_id = sha1(crypt($member['username']));
        $db->updateRow('bbs_members', array('username' => $username), array('session_id' => $session_id, 'lastactivity' => getTime()));
        $cookie->cTime = (int)$cookielength;
        $cookie->WriteCookie(array('session' => $session_id));
        header('location: ' . APP_URL);
        exit;
    }
    else
        $error = 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง กรุณาตรวจสอบอีกครั้ง';
}
$pagetitle = 'เข้าสู่ระบบ';
$smarty->assign('pageid', 'signin');
$smarty->assign('pagetitle', $pagetitle);
$smarty->display('header.html');
$smarty->assign('error', $error);
$smarty->display('login.html');
require_once ('sidebar.php');
require_once ('footer.php');

?>